"""
    Certificate delivery contoller.
"""

from turbogears import widgets, validators
from mnemoply import model
from mnemoply.mass_actions import MassAction, MassActionForm
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController

class MASetDate(MassAction):
    target_class = model.CertificateDelivery
    name = "change_certificate_delivery_date"
    title = _("Change date")

    def call(self, objs, date):
        for cd in objs:
            cd.date = date

    form = MassActionForm(
        name=name,
        fields=[
            widgets.CalendarDatePicker(
                label = _('Choose date'),
                name = 'date',
                calendar_lang = 'ru',
                format = '%d.%m.%Y',
                validator = validators.DateTimeConverter(format="%d.%m.%Y"))
            ])

class MASetTakeInTrue(MassAction):
    target_class = model.CertificateDelivery
    name = "set_take_in_true"
    title = _("Pass certificate")

    def call(self, objs):
        for cd in objs:
            cd.takeIn = True

class CertificateDeliveryController(LookupController, CRUDController):
    model_object = model.CertificateDelivery
    lookup_columns = [
        TableCol(name='student', caption=_("Student")),
        TableCol(name='certificate', caption=_("Certificate")),
        TableCol(name='date', caption=_("Date")),
        TableCol(name='take_in_str', caption=_("Take in?"), hidden=True),
    ]
    view_fields = [
        'studentID',
        'certificateID',
        'date',
        'takeIn',
    ]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('certificate_delivery_lookup'),
            columns=self.lookup_columns,
            mass_actions_class=self.model_object,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = CertificateDeliveryController()
